﻿using OestsCommon.Model;
using System;
using System.Collections.Generic;
using System.Data;

namespace OestsDataBase
{
    public static class PostDAL
    {
        //岗位
        public static void CreateTable()
        {
            string sqlCheck = "if object_id( 'T_Post') is not null select 1 else select 0";
            if ((int)SqlHelper.ExecuteScalar(sqlCheck) == 0)
            {
                string sqlCreacte = @"CREATE TABLE T_Post (id bigint primary key ,name NVARCHAR(50) not null UNIQUE)";

                SqlHelper.ExecuteNonQuery(sqlCreacte);
            }
        }

        /// <summary>
        /// 获取所有的岗位
        /// </summary>
        /// <returns></returns>
        public static List<PostModel> GetAllPost()
        {
            string strSql = string.Format("select * from T_Post ");
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            List<PostModel> list = new List<PostModel>();
            foreach (DataRow dr in dt.Rows)
            {
                PostModel m = ReadData(dr);
                list.Add(m);
            }
            return list;
        }

        private static PostModel ReadData(DataRow dr)
        {
            PostModel model = new PostModel();
            model.Id = Int32.Parse(dr["id"].ToString());
            model.Name = dr["name"].ToString();
            return model;
        }

        public static PostModel GetPostModel(int postId)
        {
            string strSql = string.Format("select * from T_Post where id= " + postId);
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            if (dt.Rows.Count > 0)
                return ReadData(dt.Rows[0]);
            return null;
        }

    }
}
